package com.vst.test.generate.structure.springmvc.plugin.analizador;

import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CLASS_ENTIDAD;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.CARPETA_DOMINIO;
import static com.vst.test.generate.structure.springmvc.plugin.util.SringProjectMainResource.DONT;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;

import com.vst.test.generate.structure.springmvc.plugin.util.EstructureObject;

public class AnalizadorEntidadesDominio {

	private boolean existenEntidades = false;
	private EstructureObject estructureObjectGeneralProject = null;
	private List<ICompilationUnit> listaICompilationUnits = null;
	private AnalizarrecursosInProject analizarrecursosInProject = null;
	
	public AnalizadorEntidadesDominio(AnalizarrecursosInProject analizador) {
		this.analizarrecursosInProject  = analizador;
		this.estructureObjectGeneralProject = analizador.getEstructureObjectGeneralProject();		
	}

	public void scanDominio() throws JavaModelException {
		listaICompilationUnits = new ArrayList<ICompilationUnit>();
		String strpackageDominio = estructureObjectGeneralProject.getPackageBase()+DONT+CARPETA_DOMINIO;
		System.out.println(strpackageDominio);
		IPackageFragment iPackageFragment = analizarrecursosInProject.generateIPackage(strpackageDominio, false);
		if(iPackageFragment==null){
			MessageDialog.openInformation(null,"Generate Source Java Service","No existe el paquete "+strpackageDominio);			
			return;
		}
		ICompilationUnit[] compilationUnit =  iPackageFragment.getCompilationUnits();
		IType[] iTypes = null;
		System.out.println("count compilationUnit:"+compilationUnit.length);
		if(compilationUnit.length>0){
			for (ICompilationUnit iCompilationUnit : compilationUnit) {
				iTypes = iCompilationUnit.getTypes();
				for (IType iType : iTypes) {
					ITypeHierarchy typeHier= iType.newSupertypeHierarchy(null);
					IType[] superInterfaces= typeHier.getAllInterfaces();
					for (IType iType2 : superInterfaces) {
						if(iType2.getElementName().equals(CLASS_ENTIDAD)){
							listaICompilationUnits.add(iCompilationUnit);
						}
					}				
				}				
			}
		}else{
			MessageDialog.openInformation(null,"Generate Source Java Service","No hay clases que extendan de la interfaz "+CLASS_ENTIDAD);			
			return;
		}
		if(listaICompilationUnits.size()>0){
			existenEntidades = true;
		}else{
			existenEntidades = false;
		}		
	}
	public boolean isExistenEntidades() {
		return existenEntidades;
	}
	public void setExistenEntidades(boolean existenEntidades) {
		this.existenEntidades = existenEntidades;
	}
	public List<ICompilationUnit> getListaICompilationUnits() {
		return listaICompilationUnits;
	}

	public void setListaICompilationUnits(
			List<ICompilationUnit> listaICompilationUnits) {
		this.listaICompilationUnits = listaICompilationUnits;
	}

	public boolean existenEntidades() {		
		return existenEntidades;
	}


}
